﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanderlaarsBonnen;

namespace HanderlaarsBonnen.Controllers
{ 
    public class VoucherController : Controller
    {
        private VoucherDbEntities1 db = new VoucherDbEntities1();

        //
        // GET: /Voucher/

        public ViewResult Index()
        {
            var voucher = db.Voucher.Include("BTC");
            return View(voucher.ToList());
        }

        //
        // GET: /Voucher/Details/5

        public ViewResult Details(int id)
        {
            Voucher voucher = db.Voucher.Single(v => v.VoucherId == id);
            return View(voucher);
        }

        //
        // GET: /Voucher/Create

        public ActionResult Create()
        {
            ViewBag.BtcId = new SelectList(db.BTC, "BtcId", "BtcNaam");
            return View();
        } 

        //
        // POST: /Voucher/Create

        [HttpPost]
        public ActionResult Create(Voucher voucher)
        {
            if (ModelState.IsValid)
            {
                db.Voucher.AddObject(voucher);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.BtcId = new SelectList(db.BTC, "BtcId", "BtcNaam", voucher.BtcId);
            return View(voucher);
        }
        
        //
        // GET: /Voucher/Edit/5
 
        public ActionResult Edit(int id)
        {
            Voucher voucher = db.Voucher.Single(v => v.VoucherId == id);
            ViewBag.BtcId = new SelectList(db.BTC, "BtcId", "BtcNaam", voucher.BtcId);
            return View(voucher);
        }

        //
        // POST: /Voucher/Edit/5

        [HttpPost]
        public ActionResult Edit(Voucher voucher)
        {
            if (ModelState.IsValid)
            {
                db.Voucher.Attach(voucher);
                db.ObjectStateManager.ChangeObjectState(voucher, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BtcId = new SelectList(db.BTC, "BtcId", "BtcNaam", voucher.BtcId);
            return View(voucher);
        }

        //
        // GET: /Voucher/Delete/5
 
        public ActionResult Delete(int id)
        {
            Voucher voucher = db.Voucher.Single(v => v.VoucherId == id);
            return View(voucher);
        }

        //
        // POST: /Voucher/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Voucher voucher = db.Voucher.Single(v => v.VoucherId == id);
            db.Voucher.DeleteObject(voucher);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}